<?php
/**
 * CakeCMS后台 - 管理员角色model
 * 
 * @author zzdboy
 * @version 1.0
 */
class AdminRole extends AppModel {
	public $name = 'AdminRole';
	public $useTable = 'admin_role';
	public $primaryKey = 'roleid';
	
	/**
	 * 根据角色ID获取角色相关信息
	 *
	 * @param int $roleid        	
	 */
	public function GetAdminRoleByID($roleid) {
		$RoleInfo = $this->find ( 'first', array (
				'conditions' => array (
						'roleid' => $roleid 
				) 
		) );
		if ($RoleInfo) {
			// 调用角色权限类
			App::import ( 'Model', 'AdminRolePriv' );
			$RolePrivModel = new AdminRolePriv ();
			$RolePrivInfo = $RolePrivModel->GetAdminRolePrivByID ( $RoleInfo ['AdminRole'] ['roleid'] );
			$RoleInfo ['AdminRole'] ['data'] = $RolePrivInfo ['AdminRolePriv'] ['data'];
		}
		return $RoleInfo;
	}
	
	/**
	 * 根据条件获取角色列表
	 *
	 * @param array $conditions        	
	 */
	public function GetAdminRoleByAll($conditions = array()) {
		$result = $this->find ( 'all', array (
				'conditions' => $conditions,
				'order' => 'roleid ASC' 
		) );
		return $result;
	}
	
	/**
	 * 添加角色
	 *
	 * @param array $data        	
	 * @return boolean
	 */
	public function add($data) {
		if (! is_array ( $data )) {
			return false;
		}
		if ($this->save ( $data )) {
			return true;
		}
		return false;
	}
	
	/**
	 * 编辑角色
	 *
	 * @param array $data        	
	 */
	public function edit($data) {
		if (! is_array ( $data )) {
			return false;
		}
		
		if ($this->save ( $data )) {
			return true;
		}
		return false;
	}
	
	/**
	 * 删除角色
	 *
	 * @param int $roleid        	
	 */
	public function del($roleid) {
		if ($this->delete ( $roleid )) {
			// 调用角色权限类删除角色相关权限
			App::import ( 'Model', 'AdminRolePriv' );
			$AdminRolePriv = new AdminRolePriv ();
			$AdminRolePriv->del ( $roleid );
			return true;
		}
		return false;
	}
}
?>